import {
  getProvinces,
  bankList,
  bankBranchInfo,
  addBank,
  bankcardList,
  upBank
} from "../../../../api.js"
Page({

  /**
   * 页面的初始数据
   */
  data: {
    fotter_btn: '保存',
    province_index: -1,
    province_lis: [],
    bankList_index: -1,
    bankList: [],
    city_lis: [],
    city_index: -1,
    bankBranch_lis: [],
    bankBranch_index: -1,
    form: {
      name: '',
      bankcard_id: '',
      opening_bank: '请选择',
      opening_bank_branch_code: '',
      opening_bank_branch: '请选择',
      opening_bank_code: '',
      provinceCode: '',
      province_title: '请选择',
      cityCode: '',
      city_title: '请选择',
      is_default: 1
    },
    modify_id: '',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this
    if (options.id) {
      console.log('传参')
      bankcardList(
        getApp().globalData.Store.token,
        options.id
      ).then(res => {
        console.log(res)
        if (res.data.code == 666) {
          res.data.data[0].opening_bank_branch = res.data.data[0].opening_bank_branch.replace(res.data.data[0].opening_bank + '股份有限公司', '')
          var textnum_text = res.data.data[0].opening_bank + '股份有限公司'
          bankBranchInfo(
            res.data.data[0].provinceCode,
            res.data.data[0].cityCode,
            res.data.data[0].opening_bank_code
          ).then(res => {
            if (res.data.code == 666) {
              console.log(res)
              for (var i = 0; i < res.data.data.length; i++) {
                res.data.data[i].name = res.data.data[i].name.replace(textnum_text, '')
              }
              that.setData({
                bankBranch_lis: res.data.data
              })
            }
          })
          that.setData({
            modify_id: options.id,
            form: res.data.data[0],
          })
        }
      })

    }
    getProvinces().then(res => {
      if (res.data.code == 666) {
        if (that.data.modify_id) {
          var li = res.data.data
          for (var i = 0; i < li.length; i++) {
            if (li[i].label == that.data.form.province_title) {
              that.setData({
                city_lis: li[i].children,
                province_index: i
              })
              console.log(that.data.province_index)
            }
          }
        }
        that.setData({
          province_lis: res.data.data
        })
      }
    })
    bankList().then(res => {
      if (res.data.code == 666) {
        that.setData({
          bankList: res.data.data
        })
      }
    })
  },
  bindprovinceLis(e) {
    console.log(e)
    var ind = e.detail.value
    var that = this
    that.data.form.provinceCode = that.data.province_lis[ind].value
    that.data.form.cityCode = '',
      that.data.form.opening_bank_code = '',
      that.data.form.opening_bank_branch = '请选择',
      that.data.form.opening_bank_branch_code = '',
      that.data.form.opening_bank = '请选择'
    that.setData({
      province_index: ind,
      city_lis: that.data.province_lis[ind].children,
      form: that.data.form,
      city_index: -1,
      bankList_index: -1,
      bankBranch_index: -1
    })
  },
  bindcity(e) {
    var that = this
    var ind = e.detail.value
    if (that.data.province_index == -1 && !that.data.modify_id) {
      wx.showToast({
        title: '请先选择开户省',
        icon: 'none'
      })
      return
    } else {
      that.data.form.cityCode = that.data.city_lis[ind].value,
        that.data.form.opening_bank_code = '',
        that.data.form.opening_bank_branch = '请选择',
        that.data.form.opening_bank_branch_code = '',
        that.data.form.opening_bank = '请选择'
      that.setData({
        city_index: ind,
        form: that.data.form,
        bankList_index: -1,
        bankBranch_index: -1
      })
    }
  },
  bindbankList(e) {
    var that = this
    var ind = e.detail.value
    if (that.data.city_index == -1 && !that.data.modify_id) {
      wx.showToast({
        title: '请先选择开户市',
        icon: 'none'
      })
      return
    } else {
      wx.showLoading({
        title: '正在查询分行',
      })
      that.data.form.opening_bank = that.data.bankList[ind].name,
        that.data.form.opening_bank_code = that.data.bankList[ind].code,
        that.data.form.opening_bank_branch = '请选择',
        that.data.form.opening_bank_branch_code = '',
        bankBranchInfo(
          that.data.form.provinceCode,
          that.data.form.cityCode,
          that.data.form.opening_bank_code
        ).then(res => {
          if (res.data.code == 666) {
            wx.hideLoading()
            var textnum_text = that.data.form.opening_bank + '股份有限公司'
            for (var i = 0; i < res.data.data.length; i++) {
              res.data.data[i].name = res.data.data[i].name.replace(textnum_text, '')
            }
            that.setData({
              bankBranch_lis: res.data.data
            })
          }
        })
      that.setData({
        bankList_index: ind,
        form: that.data.form,
        bankBranch_index: -1
      })
    }
  },
  bindPickerChange(e) {
    var that = this
    var ind = e.detail.value
    if (that.data.bankList_index == -1 && !that.data.modify_id) {
      wx.showToast({
        title: '请先选择开户银行总行',
        icon: 'none'
      })
      return
    } else {
      that.data.form.opening_bank_branch = that.data.bankBranch_lis[ind].name,
        that.data.form.opening_bank_branch_code = that.data.bankBranch_lis[ind].id
      that.setData({
        form: that.data.form,
        bankBranch_index: ind
      })
    }
  },
  moren() {
    var that = this
    that.setData({
      'form.is_default': that.data.form.is_default == 0 ? 1 : 0
    })
  },
  formSubmit(e) {
    var form = e.detail.value
    var that = this
    if (!form.name) {
      wx.showToast({
        title: '请填写姓名',
        icon: 'none'
      })
      return
    }
    if (!form.card) {
      wx.showToast({
        title: '请填写卡号',
        icon: 'none'
      })
      return
    }
    if (that.bankBranch_index == -1) {
      wx.showToast({
        title: '请选择分行',
        icon: 'none'
      })
      return
    }
    that.data.form.name = form.name
    that.data.form.bankcard_id = form.card
    if (that.data.modify_id) {
      upBank(
        getApp().globalData.Store.token,
        that.data.modify_id,
        { ...that.data.form }
      ).then(res => {
        console.log(res)
        wx.navigateBack({
          delta: 1
        })
      })
    } else {
      addBank(
        getApp().globalData.Store.token, {
          ...that.data.form
        }
      ).then(res => {
        if (res.data.code == 666) {
          wx.navigateBack({
            delta: 1
          })
        }
      })
    }
  },
  nameBlur(e) {
    var val = e.detail.value
    this.setData({
      'form.name': val
    })
  },
  cardBlur(e) {
    var val = e.detail.value
    this.setData({
      'form.bankcard_id': val
    })
  },
  onShow: function () {
    getApp().startpage(new Date().getTime())
  },
  onHide() {
    getApp().endpage(new Date().getTime())
  },
})